using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.Core.Domain.News;
using ITS.DAL.News;
using ITS.Factory;

namespace ITS.Services.News
{
    public class NewsService : INewsService
    {
        private readonly INewsRepository _newsRepository;

        public NewsService()
        {
            _newsRepository = NinjectRepositoriesFactory.Get<INewsRepository>();
        }
        public IList<NewsItem> GetNewsItemsByTagID(int TagID)
        {
            return _newsRepository.GetNewsItemsByTagID(TagID);
        }


        public IEnumerable<NewsCategory> GetAllCategories()
        {
            return _newsRepository.GetAllCategories();
        }


        public string GetDescription(int p, string strCulture)
        {
            return _newsRepository.GetDescription(p, strCulture);
        }


        public bool HasCategory(NewsItem news, int iTmp)
        {
            return _newsRepository.HasCategory(news, iTmp);
        }


        public void AddCategory(int NewsID, int CategoryID)
        {
            _newsRepository.AddCategory(NewsID, CategoryID);
        }


        public NewsCategory GetCategoryByID(int CategoryID)
        {
            return _newsRepository.GetCategoryByID(CategoryID);
        }


        public void AddNewsItem(NewsItem news)
        {
            _newsRepository.AddNewsItem(news);
        }


        public NewsItem GetNewsItemByID(int p)
        {
            return _newsRepository.GetNewsItemByID(p);
        }


        public void RemoveCategory(int NewsItemID, int CategoryID)
        {
            _newsRepository.RemoveCategory(NewsItemID, CategoryID);
        }

        public IList<NewsItem> GetAllNewsItems()
        {
            return _newsRepository.GetAllNewsItems();
        }

        public void SaveNewsTags(NewsItem newsItem, string[] productTags)
        {
            if (newsItem == null)
                throw new ArgumentNullException("news");

            //product tags
            var existingProductTags = newsItem.NewsTags.OrderByDescending(pt => pt.NewsCount).ToList();
            var newsTagsToDelete = new List<NewsTag>();
            foreach (var existingProductTag in existingProductTags)
            {
                bool found = false;
                foreach (string newProductTag in productTags)
                {
                    if (existingProductTag.Name.Equals(newProductTag, StringComparison.InvariantCultureIgnoreCase))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    newsTagsToDelete.Add(existingProductTag);
                }
            }
            foreach (var newsTag in newsTagsToDelete)
            {
                newsItem.NewsTags.Remove(newsTag);
                //ensure product is saved before updating totals
                _newsRepository.Save();
                UpdateNewsTagTotals(newsTag);
            }
            foreach (string newTagName in productTags)
            {
                NewsTag newsTag = null;
                var newsTag2 = GetNewsTagByName(newTagName);
                if (newsTag2 == null)
                {
                    //add new product tag
                    newsTag = new NewsTag()
                    {
                    Name = newTagName,
                    NewsCount = 0
                    };
                    InsertNewsTag(newsTag);
                }
                else
                {
                    newsTag = newsTag2;
                }
                if (!newsItem.NewsTagExists(newsTag.TagID))
                {
                    _newsRepository.AddTag(newsItem.NewsItemID, newsTag.TagID);
                    UpdateNewsTagTotals(newsTag);
                }
            }
        }

        /*tag*/
        public void UpdateNewsTagTotals(NewsTag tag)
        {
            if (tag == null)
                throw new ArgumentNullException("Update null tag");

            int newTotal = tag.NewsItems.Count;

            if (newTotal > 0)
            {
                tag.NewsCount = newTotal;
                _newsRepository.Update(tag);
            }
            else
            {
                _newsRepository.Delete(tag);
            }
        }

        public void InsertNewsTag(NewsTag tag)
        {
            if (tag == null)
                throw new ArgumentNullException("Insert null tag");

            _newsRepository.Insert(tag);
        }

        public NewsTag GetNewsTagByName(string Name)
        {
            return _newsRepository.GetNewsTagByName(Name);
        }

        public IList<NewsTag> GetAllNewsTag()
        {
            return _newsRepository.GetAllNewsTags();
        }

        public NewsTag GetNewsTagByID(int TagID)
        {
            return _newsRepository.GetNewsTagByTagID(TagID);
        }

        public void DeleteNewsTag(NewsTag tag)
        {
            if (tag == null)
                throw new ArgumentNullException("Delete null tag");

            _newsRepository.Delete(tag);
        }

        public void UpdateNewsTag(NewsTag tag)
        {
            if (tag == null)
                throw new ArgumentNullException("Update null tag");

            _newsRepository.Update(tag);
        }
        /*end-tags*/
    }
}
